﻿using jinmaMesProxy.Excep;
using Microsoft.Data.SqlClient;

namespace jinmaMesProxy.DataAccess
{
    public static class SimpleSqlHelper
    {
        public static (int,int) GetId(string app)
        {
            var instance = ServiceLoader.GetService<ErpDbInstance>();
            using (var cmd = instance.MakeQueryCommand("select top 1 iFatherId,iChildId from UFSystem.dbo.UA_Identity where cacc_id = @p1 and cVouchType = @p2 ",
                new KeyValuePair<string, object?>("@p1", RuntimeVariable.ACCID),
                new KeyValuePair<string, object?>("@p2", app)))
            {
                using (var reader = cmd.ExecuteReader())
                {
                    if (reader.Read())
                    {
                        return (reader.GetInt32(0), reader.GetInt32(1));
                    }
                }
            }
            throw new MyException($"{app}单据没有id策略");
        }
        public static void WriteId(string app,int id,int did)
        {
            var instance = ServiceLoader.GetService<ErpDbInstance>();
            using (var cmd = instance.MakeQueryCommand("update UFSystem.dbo.UA_Identity set iFatherId = @p1,iChildId =@p2  where cacc_id = @p3 and cVouchType = @p4 ",
                new KeyValuePair<string, object?>("@p1", id),
                new KeyValuePair<string, object?>("@p2", did),
                new KeyValuePair<string, object?>("@p3", RuntimeVariable.ACCID),
                new KeyValuePair<string, object?>("@p4", app)))
            {
                try
                {
                    cmd.ExecuteNonQuery();
                }
                catch (Exception ex)
                {
                    throw new MyException(ex);
                }
            }
        }

        public static int GetTempId(string cardnum)
        {
            var instance = ServiceLoader.GetService<ErpDbInstance>();
            using (var cmd = instance.MakeQueryCommand("select top 1 def_id from vouchers_base where cardnumber = @p1 ",
                new KeyValuePair<string, object?>("@p1", cardnum)))
            {
                using (var reader = cmd.ExecuteReader())
                {
                    if (reader.Read())
                    {
                        return reader.GetInt32(0);
                    }
                }
            }
            throw new MyException($"{cardnum}单据没有默认模板");
        }

        public static string CheckWarehouse(string code)
        {
            var instance = ServiceLoader.GetService<ErpDbInstance>();
            using (var cmd = instance.MakeQueryCommand("select cwhname from Warehouse where cwhcode = @p1 ",
                new KeyValuePair<string, object?>("@p1", code)))
            {
                using (var reader = cmd.ExecuteReader())
                {
                    if (reader.Read())
                    {
                        return reader.GetString(0);
                    }
                }
            }
            throw new MyException($"{code}的仓库不存在");
        }

        public static bool CalQtyWhenApproved()
        {
            var instance = ServiceLoader.GetService<ErpDbInstance>();
            using (var cmd = instance.MakeQueryCommand("select cvalue  from AccInformation  where cname = 'bOtherInCheck'"))
            {
                using (var reader = cmd.ExecuteReader())
                {
                    if (reader.Read())
                    {
                        return reader.GetString(0)=="True";
                    }
                }
            }
            throw new MyException("无法确认影响现存量时机");
        }
    }
}
